var oTable;
var modal;
var CommissionSetModule = function() {
	var editable = function() {
		var _url = window.location.pathname;
		var detailFlag = _url.indexOf("detail") != -1 ? true : false;

		return {
			init : function() {
				oTable = $('#modelEditable_comm')
						.dataTable(
								{
									"autoWidth" : false,
									"lengthMenu" : [ [ 10, 15, 20, 50, -1 ],
											[ 10, 15, 20, 50, "All" ] ],
									"displayLength" : -1,
									"language" : {
										"lengthMenu" : "每页 _MENU_ 条记录",
										"paginate" : {
											"previous" : "上一页",
											"next" : "下一页"
										},
										"info" : "",// "从 _START_ 到 _END_ 条，总记录为
													// _TOTAL_ 条",
										"infoEmpty" : "",// 记录数为0",
										"sZeroRecords" : "找不到相关数据"
									},
									"bPaginate" : false, // 是否显示分页
									"bLengthChange" : false, // 每页显示的记录数
									"columnDefs" : [ {
										"data" : null,
										"sortable" : false,
										"defaultContent" : "",
										"targets" : [ 0 ]
									}, {
										"data" : null,
										"sortable" : false,
										"defaultContent" : "",
										"targets" : [ 1 ]
									}, {
										"data" : null,
										"sortable" : false,
										"defaultContent" : "",
										"targets" : [ 2 ]
									} ],
									"columns" : [ {
										"data" : "groupName"
									}, {
										"data" : null
									}, {
										"data" : "employeeName"
									}, {
										"data" : null
									} ],
									"filter" : false,
									"processing" : false,
									"serverSide" : true,
									"ajaxSource" : ctx
											+ "/product/commissionList",
									"serverMethod" : "post",
									"fnRowCallback" : function(nRow, aData,
											displayIndex) {
										nRow.children[0].id = aData.employeeId;

										if (aData.groupName) {
											nRow.children[1].id = (aData.dividendRate * 100)
													.toFixed(2);
											nRow.children[1].name = (aData.commissionRate * 100)
													.toFixed(2);
											// 张三(工号：10000) 管理提成2% 佣金3%

											$('td:eq(1)', nRow)
													.html(
															aData.employeeName
																	+ "&nbsp;(工号："
																	+ aData.employeeNo
																	+ ") 管理提成"
																	+ toPercent(aData.dividendRate)
																	+ "&nbsp;&nbsp;佣金"
																	+ toPercent(aData.commissionRate));
											
											$('td:eq(2)', nRow).html("");
											var opera = '<a class="dividendSet fa fa-jpy fa-lg" title="设置管理提成" href="javascript:;"></a>&nbsp;&nbsp;'
													+ '<a class="commissionSet fa fa-cog fa-lg" title="佣金设置" href="javascript:;"></a>';
											$('td:eq(3)', nRow).html(opera);
											if (!detailFlag) {
												$('td:eq(3)', nRow).html(opera);
											} else {
												$('td:eq(3)', nRow).remove();
											}
										} else {
											nRow.children[1].name = (aData.commissionRate * 100)
													.toFixed(2);
											$('td:eq(2)', nRow)
													.html(
															aData.employeeName
																	+ "&nbsp;(工号："
																	+ aData.employeeNo
																	+ ") 佣金"
																	+ toPercent(aData.commissionRate));
											// var opera = '<button
											// class="commissionSet btn
											// btn-outline btn-primary"
											// type="button">佣金设置</button>';
											var opera = '<a class="commissionSet fa fa-cog fa-lg" title="佣金设置" href="javascript:;"></a>';
											if (!detailFlag) {
												$('td:eq(3)', nRow).html(opera);
											} else {
												$('td:eq(3)', nRow).remove();
											}
											
											
										}

										if (aData.employeeName == "未设置") {
											$('td:eq(1)', nRow).html("未设置");
											$('td:eq(3)', nRow).html("");
										}
										
										return nRow;
									},
									"fnServerData" : function(ajaxSource,
											dataSet, fnCallback) {
										var start;
										var pageSize;
										for (var i = 0; i < dataSet.length; i++) {
											s = dataSet[i];
											if (s.name == 'iDisplayStart') {
												start = s.value;
											} else if (s.name == 'iDisplayLength') {
												pageSize = s.value;
											}
										}

										$
												.ajax({
													"contentType" : "application/json; charset=UTF-8",
													"dataType" : 'json',
													"type" : "POST",
													"url" : ajaxSource,
													"data" : '{"id":"'
															+ $("#productId")
																	.val()
															+ '","start":'
															+ start
															+ ',"pageSize":'
															+ pageSize + "}",
													"success" : fnCallback
												});
									},
								});
				// 删除
				$('#modelEditable_comm').on("click", "a.delete", function(e) {
					e.preventDefault();
					var obj = $(this);
					bootbox.confirm("您确定删除这条记录?", function(result) {
						if (result) {
							var nRow = obj.parents('tr')[0];
							var id = nRow.children[0].id;
							$.ajax({
								type : "post",
								url : ctx + "/employee/delete",
								data : "id=" + id,
								dataType : "json",
								success : function(msg) {
									if (msg.code == 1) {
										bootbox.alert("删除用户信息成功！");
										oTable.fnDeleteRow(nRow);
									} else {
										bootbox.alert("删除用户信息失败！");
									}
								},
								error : function(msg) {
									bootbox.alert(msg.message);
								}
							});
						}
					});
				});

				// 明细
				$('#modelEditable_comm').on("click", "a.dividendSet",
						(function(e) {
							e.preventDefault();

							var nRow = $(this).parents('tr')[0];
							employeeId = nRow.children[0].id;
							// window.location.href = "product/detail/" + id;
							$("#d_title").text("设置管理提成");
							$("#d_old_val").text("原提成(%)");
							$("#old_val").val(nRow.children[1].id);
							$("#d_new_val").text("新提成(%)");
							$("#new_val").val(nRow.children[1].id);
							modal = $('#updateModel_comm').modal('show');
						}));

				$('#modelEditable_comm').on("click", "a.commissionSet",
						(function(e) {
							e.preventDefault();

							var nRow = $(this).parents('tr')[0];
							employeeId = nRow.children[0].id;
							// window.location.href = "product/relatedFile/" +
							// id;

							$("#d_title").text("设置佣金");
							$("#d_old_val").text("原佣金(%)");
							$("#old_val").val(nRow.children[1].name);
							$("#d_new_val").text("新佣金(%)");
							$("#new_val").val(nRow.children[1].name);
							modal = $('#updateModel_comm').modal('show');
						}));

				// $('#test__').click(function() {
				// alert()
				// oTable.fnClearTable();
				// });
				//
				// $('#addBtn').click(function() {
				// window.location.href = "product/add";
				// });
			}

		};

	}();

	return {
		init : function() {
			editable.init();
		}
	};
}();
